const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const {
  $Toast
} = require('../../components/base/base');
import ad from '../../utils/ad.js';
import city from '../../utils/city.js';
var contactormodel = null;
var check;

Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    home: true,
    scrollHeight: app.globalData.windowHeight - app.globalData.navHeight,
    goTop: true,
    done: false,
    options1: city,
    value1: [],
    userName: '',
    mobile: '',
    address: '',
    isDefault: null
  },

  newEdit: function() {
    util.navigateTo('../editaddress/editaddress')
  },

  onOpen1() {
    this.setData({
      visible1: true
    })
  },

  onClose1() {
    this.setData({
      visible1: false
    })
  },

  onChange1(e) {
    this.setData({
      areainfo: e.detail.options.map((n) => n.label).join(' '),
      done: e.detail.done
    })

  },

  scroll: function(e) {
    var that = this;
    var listIndex = (e.detail.scrollTop + this.data.screenHeight) / (this.data.screenWidth * 0.4)
    if (e.detail.scrollTop > app.globalData.navHeight + 40) {
      this.setData({
        goTop: false
      });
      this.setData({
        listIndex: listIndex
      })
    } else {
      this.setData({
        goTop: true
      });
    }

  },

  //返回顶部
  goScrolltop: function(e) {
    var that = this;
    var top = that.data.scrollTop;
    if (top == 1) {
      top = 0;
    } else {
      top = 1;
    }

    that.setData({
      scrollTop: top
    })

  },

  userName: function(e) {
    var userName = e.detail.value
    this.setData({
      userName: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
  },

  mobile: function(e) {
    var mobile = e.detail.value
    this.setData({
      mobile: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
  },

  address: function(e) {
    var address = e.detail.value
    this.setData({
      address: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
  },

  checkAlls: function(e) {
    var that = this;
    if (e.detail.value == '') {
      check= false
    } else {
      check= true
    }
  },

  randomn(n) {
    if (n > 21)
      return null
    return parseInt((Math.random() + 1) * Math.pow(10, n - 1))
  },

  areaSave: util.throttle(function(e) {
    var that = this;
    var userName = that.data.userName;
    var mobile = that.data.mobile;
    var areainfos = that.data.done;
    var address = that.data.address;

    if (userName == "" || userName == undefined || userName == null || userName.length == 0) {
      $Toast({
        content: '请输入收货人!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (mobile == "" || mobile == undefined || mobile == null || mobile.length == 0) {
      $Toast({
        content: '请输入手机号!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (mobile.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(mobile)) {
      $Toast({
        content: '手机号格式不正确!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (areainfos == false) {
      $Toast({
        content: '请选择收货地址!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (address == "" || address == undefined || address == null || address.length == 0) {
      $Toast({
        content: '请输入详细地址!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else {
      var arr = wx.getStorageSync('contactormodel') || [];
      contactormodel = {
        userName: userName,
        mobile: mobile,
        areainfo: that.data.areainfo,
        address: address,
        isDefault: false,
        id: that.randomn(6)
      }

      if (check != true) {
        arr.push(contactormodel);
        wx.setStorageSync('contactormodel', arr);
      } else {
        for (var i = 0; i < arr.length; i++) {
          arr[i].isDefault = false
        }
        arr.push(contactormodel);
        arr[arr.length - 1].isDefault = true;
        wx.setStorageSync('contactormodel', arr);

      }

      $Toast({
        content: '新建收货地址成功!',
        icon: 'success',
        duration: 1
      });

      wx.navigateBack({
        delta: 1
      })

    }

  }, 1000),

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      areainfo: '请选择收货地址'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }

})